/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package NamNH;

import Entity.Account;
import Entity.Document;
import Entity.Event;
import VietDQ.VietDQSessionBeanLocal;
import java.io.IOException;
import java.io.PrintWriter;
import javax.jms.Session;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 *

 */
public class NamNHServlet extends HttpServlet {
   
    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
             HttpSession session = request.getSession(true);
            String action = request.getParameter("action");
            Context ctx = new InitialContext();
            VietDQSessionBeanLocal VLocal = (VietDQSessionBeanLocal) ctx.lookup("VietDQSessionBeanLocalJNDI");
            NamNHSessionBeanLocal NLocal = (NamNHSessionBeanLocal) ctx.lookup("NamNHSessionBeanLocalJNDI");
          if(action.equals("Pay"))
          {
             Event ObjEve = VLocal.findEventByPK(Integer.parseInt(request.getParameter("EvID")));
             request.getSession().setAttribute("Eve", ObjEve);
             request.getRequestDispatcher("Payment.jsp").forward(request, response);
          }else if(action.equals("Payment"))
          {

                 if(NLocal.insertPayment(Integer.parseInt(request.getParameter("txtEVID")), request.getParameter("txtUserID"), request.getParameter("txtPrice")))
                 {
                     NLocal.UploadDocument(Integer.parseInt(request.getParameter("txtEVID")), request.getParameter("txtUserID"));
                     request.getRequestDispatcher("NamNHServlet?action=USVIEWEV").forward(request, response);
                 }
                 
          }
            else if(action.equals("USVIEWEV"))
          {
                Account objA = (Account)session.getAttribute("User_Session");
                String us = objA.getUsername();
                AccountDocument listDTO = NLocal.historyDocument(us);
                session.setAttribute("listEVAccount", listDTO);
                request.getRequestDispatcher("UsManageEvent.jsp").forward(request, response);
          }
          else if(action.equals("goUpload"))
          {
                int idd = Integer.parseInt(request.getParameter("DocId"));
                Document objDoc = NLocal.findDocumentByPK(idd);
                session.setAttribute("objDocu", objDoc);
                request.getRequestDispatcher("UploadDocument.jsp").forward(request, response);
          }
          else if(action.equals("GoSetMark"))
          {
                Document objDOC = NLocal.findDocumentByPK(Integer.parseInt(request.getParameter("DOCID")));
                session.setAttribute("setMar", objDOC);
                request.getRequestDispatcher("SetMark.jsp").forward(request, response);
          }
          else if(action.equals("Set Mark"))
          {
                int idDoc = Integer.parseInt(request.getParameter("txtIdDoc"));
                String mar = request.getParameter("txtMark");
                int idEv = Integer.parseInt(request.getParameter("txtIdEv"));
                if(NLocal.setMarkDocument(idDoc, mar))
                {
                    request.getRequestDispatcher("VietDQServlet?action=GoListDoc&EventID="+idEv).forward(request, response);
                }
          }
          else if(action.equals("Winner"))
          {
                int idEv = Integer.parseInt(request.getParameter("txtEvId"));
                String use = request.getParameter("txtUSWinner");
                if(NLocal.setWinner(idEv, use))
                {
                    request.getRequestDispatcher("VietDQServlet?action=GetAllEvents").forward(request, response);
                }
          }
        }catch(Exception e)
        {
            e.printStackTrace();
        } finally { 
            out.close();
        }
    } 

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    } 

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

}
